package com.jisf.driver.controller;

import com.jisf.driver.entity.vo.UserAddRequestVo;
import com.jisf.driver.entity.vo.UserListRequestVo;
import com.jisf.driver.entity.vo.UserListResponseVo;
import com.jisf.driver.entity.vo.UserUpdatePasswordRequestVo;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.Result;
import com.jisf.driver.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/driver/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @PostMapping("list")
    @PreAuthorize("hasAnyAuthority('user:list')")
    public Result<?> userList(@RequestBody UserListRequestVo userListRequestVo){
        UserListResponseVo userList = iUserService.pageUserList(userListRequestVo);
        return Result.ok(userList);
    }

    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('user:add')")
    public Result<?> userAdd(@RequestBody UserAddRequestVo userAddRequestVo){
        IErrorCode iErrorCode = iUserService.userAdd(userAddRequestVo);
        return Result.ok(iErrorCode);
    }

    @GetMapping("change/status")
    @PreAuthorize("hasAnyAuthority('user:change:status')")
    public Result<?> userChangeStatus(@RequestParam Integer id,
                                      @RequestParam String status){
        IErrorCode iErrorCode = iUserService.userChangeStatus(id, status);
        return Result.ok(iErrorCode);
    }

    @GetMapping("delete")
    @PreAuthorize("hasAnyAuthority('user:delete')")
    public Result<?> userDelete(@RequestParam Integer id){
        IErrorCode iErrorCode = iUserService.userDelete(id);
        return Result.ok(iErrorCode);
    }

    @PostMapping("update/password")
    @PreAuthorize("hasAnyAuthority('user:update:password')")
    public Result<?> updatePassword(@RequestBody UserUpdatePasswordRequestVo userUpdatePasswordRequestVo){
        IErrorCode iErrorCode = iUserService.updatePassword(userUpdatePasswordRequestVo);
        return Result.ok(iErrorCode);
    }


}
